package com.icode.dao.impl;

import com.icode.core.dto.order.PageDTO;
import com.icode.core.model.order.AlipayStatus;
import com.icode.core.model.order.Order;
import com.icode.core.model.order.ReFund;
import com.icode.core.model.user.User;
import com.icode.core.model.user.UserInfo;
import com.icode.dao.OrderDao;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-1-20
 * Time: 下午2:11
 * To change this template use File | Settings | File Templates.
 */
@Repository("orderDao")
public class OrderDaoImpl extends EntityDaoImpl implements OrderDao {

    @Override
    public void saveAndUpdateUserStatus(User user) {
        String hql = "update " + User.class.getName() + " a set a.isActivate=:isActivate where a.guid=:guid";
        Query query = currentSession().createQuery(hql);
        query.setParameter("isActivate", "true");
        query.setParameter("guid", user.guid());
        query.executeUpdate();
    }

    @Override
    public List<Order> findOrderByUsersInfo(String username) {
        UserInfo userInfo = findUsersInfoByPhoneNum(username);
        String hql = "from " + Order.class.getName() + " a where a.userInfo=:userInfo";
        Query query = currentSession().createQuery(hql);
        query.setParameter("userInfo", userInfo);
        return query.list();
    }

    @Override
    public ReFund findReFundByTradeNo(String tradeNo) {
        String hql = "from " + ReFund.class.getName() + " a where a.tradeNo=:tradeNo";
        Query query = currentSession().createQuery(hql);
        query.setParameter("tradeNo", tradeNo);
        return (ReFund) query.uniqueResult();
    }

    @Override
    public List<ReFund> findReFundByOrder(Order order) {
        String hql = "from " + ReFund.class.getName() + " a where a.order=:order";
        Query query = currentSession().createQuery(hql);
        query.setParameter("order", order);
        return query.list();
    }

    @Override
    public Order findOrderByUsersInfoTime(String username, String orderTime) {
        UserInfo userInfo = findUsersInfoByPhoneNum(username);
        String hql = "from " + Order.class.getName() + " a where a.userInfo=:userInfo and a.orderInfo.time=:time";
        Query query = currentSession().createQuery(hql);
        query.setParameter("userInfo", userInfo);
        query.setParameter("time", orderTime);
        return (Order) query.uniqueResult();
    }

    @Override
    public UserInfo findUsersInfoByPhoneNum(String phoneNum) {
        User user = findUserByUsername(phoneNum);
        String hql = "from " + UserInfo.class.getName() + " a where a.user=:user";
        Query query = currentSession().createQuery(hql);
        query.setParameter("user", user);
        UserInfo usersInfo = (UserInfo) query.uniqueResult();
        return usersInfo;
    }

    @Override
    public User findUserByUsername(String username) {
        String hql = "from " + User.class.getName() + " a where a.username=:username";
        Query query = currentSession().createQuery(hql);
        query.setParameter("username", username);
        return (User) query.uniqueResult();
    }

    @Override
    public void deleteOrdersByGuid(String guid) {
        Order order = findByGuid(Order.class, guid);
        String hql = "delete from " + ReFund.class.getName() + " a where a.order=:order";
        Query query = currentSession().createQuery(hql);
        query.setParameter("order", order);
        query.executeUpdate();
        currentSession().delete(order);
    }

    @Override
    public void paySuccessOrderByGuid(String guid, String alipayTradeNo) {
        String hql = "update " + Order.class.getName() + " a  set a.orderInfo.status =:status,a.orderInfo.alipayTradeNo=:alipayTradeNo where a.guid=:guid";
        Query query = currentSession().createQuery(hql);
        query.setParameter("status", AlipayStatus.PAY_SUCCESS);
        query.setParameter("alipayTradeNo", alipayTradeNo);
        query.setParameter("guid", guid);
        query.executeUpdate();
    }

    @Override
    public void deleteOrdersByList(String[] guidArray) {
        deleteReFundByList(guidArray);
        String hql = "delete from " + Order.class.getName() + " a where a.guid in (:guids)";
        Query query = currentSession().createQuery(hql);
        query.setParameterList("guids", guidArray);
        query.executeUpdate();
    }

    private void deleteReFundByList(String[] guidArray) {
        String hql = "from " + Order.class.getName() + " a where a.guid=:guid";
        String del = "delete from " + ReFund.class.getName() + " a where a.order=:order";
        Query query = currentSession().createQuery(hql);
        Query delete = currentSession().createQuery(del);
        for (int i = 0; i < guidArray.length; i++) {
            query.setParameter("guid", guidArray[i]);
            delete.setParameter("order", query.uniqueResult());
            delete.executeUpdate();
        }
    }

    @Override
    public void changeOrderStatusByGuid(String guid, String status) {
        String hql = "update " + Order.class.getName() + " a set a.orderInfo.status=:status where a.guid=:guid";
        Query query = currentSession().createQuery(hql);
        query.setParameter("status", status);
        query.setParameter("guid", guid);
        query.executeUpdate();
    }

    @Override
    public void changeReFundStatusByGuid(String guid, String status) {
        String hql = "update " + ReFund.class.getName() + " a set a.refundStatus=:refundStatus where a.guid=:guid";
        Query query = currentSession().createQuery(hql);
        query.setParameter("refundStatus", status);
        query.setParameter("guid", guid);
        query.executeUpdate();
    }

    @Override
    public List<ReFund> findRefundByStatus(String status) {
        String hql = "from " + ReFund.class.getName() + " a where a.refundStatus=:refundStatus";
        Query query = currentSession().createQuery(hql);
        query.setParameter("refundStatus", status);
        return query.list();
    }

    @Override
    public List<Order> findAllOrderByPage(String currentPage) {
        String hql = "from " +Order.class.getName();
        Query query =currentSession().createQuery(hql);
        query.setFirstResult((Integer.parseInt(currentPage) - 1) * PageDTO.pageOfData);
        query.setMaxResults(PageDTO.pageOfData);
        List<Order> orders = query.list();
        return  orders;
    }

}
